package com.example.day01;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.day01.databinding.ActivityMain2Binding;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity2 extends AppCompatActivity {
    private ActivityMain2Binding binding;
    private VideoMutiAdapter videoMutiAdapter;
    OkHttpClient client;
    Retrofit retrofit;
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main2);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main2);

        binding.rv.setLayoutManager(new LinearLayoutManager(this));
        videoMutiAdapter = new VideoMutiAdapter(videos);
        binding.rv.setAdapter(videoMutiAdapter);

        videoMutiAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Toast.makeText(MainActivity2.this, "点击了"+videos.get(position).getCaption(), Toast.LENGTH_SHORT).show();
            }
        });
        //点击item上的图片
        videoMutiAdapter.addChildClickViewIds(R.id.iv);
        videoMutiAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()){
                    case R.id.iv:
                        Toast.makeText(MainActivity2.this, ""+videos.get(position).getVideomainimg(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        //执行网络请求
        client = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("http://10.161.9.80:7012/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        retrofit.create(ApiService.class)
                .video()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity entity) {
                        if(entity.getCode()==200){
                            for (int i = 0; i < entity.getData().size(); i++) {
                                entity.getData().get(i).setItemVideoType(i%2);//type值的修改
                                /*videos.add(entity.getData().get(i));*/
                            }
                            videos.addAll(entity.getData());
                            videoMutiAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MainActivity2.this, "请求失败："+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}